/**
 * Licensed under:
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * @author Emanuele Gherardini (e.gherardini@yahoo.it / e.gherardini@hotmail.com)
 */
package org.apache.camel.component.coherence.queue;

import org.apache.camel.Exchange;
import org.apache.camel.impl.DefaultExchangeHolder;
import org.apache.camel.impl.DefaultProducer;

import com.oracle.coherence.patterns.messaging.MessagingSession;

public class CoherenceQueueProducer extends DefaultProducer {

	private final CoherenceQueueEndpoint queueEndpoint;
	
	public CoherenceQueueProducer(CoherenceQueueEndpoint endpoint) {
		super(endpoint);
		this.queueEndpoint = endpoint;
		log.info("CoherenceQueueProducer succesfully created: "+ this);
	}

	@Override
	public void process(Exchange exchange) throws Exception {
		/**
		 * The method publishMessage is synchronous per-messaging session.
		 * If performance bottlenecks are found, consider creating a new messaging
		 * session instead of using the one from the endpoint. 
		 */
		MessagingSession session = this.queueEndpoint.getMessagingSession();
		DefaultExchangeHolder exh = DefaultExchangeHolder.marshal(exchange);
		session.publishMessage(queueEndpoint.getQueueIdentifier(), exh);
		log.info("Published Exhange:["+exchange+"] to endpoint: " + this.queueEndpoint);
	}

	@Override
	public String toString() {
		return "CoherenceQueueProducer [queueEndpoint=" + queueEndpoint + "]";
	}

}
